<?php
/**
 * 服务器Shell调用事件
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class ShellController extends BaseController {

	/**
	 * PK模块
	 * 更新当前(正在进行的PK)续费
	 */
	public function pkMount(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		//获取当前正在进行的PK信息
		$where_pk = array();
		$where_pk['start_time'] = array('LT',date('Y-m-d H:i:s'));
		$where_pk['end_time'] = array('GT',date('Y-m-d H:i:s'));
		$dlist_pk = M('pk','sales_')->where($where_pk)->order('pkid asc')->select();
		$length_pk = sizeof($dlist_pk);
		if($dlist_pk){
			$Dlist = array();
			$dlsit_save = array();
			$where_mount = array();
			$where_mount['b.audit_res'] = 1;
			$where_mount['c.audit_res'] = 1;
			for($i=0;$i<$length_pk;$i++){
				if($dlist_pk[$i]['a_type'] == 1){
					$where_mount['b.customer_sales'] = $dlist_pk[$i]['a'];
				}else if($dlist_pk[$i]['a_type'] ==2){
					$MSector = M('\Admin\Model\GoalsModel:Sector');
					$arrSector = $MSector->getChild($dlist_pk[$i]['a']);
					if($arrSector){
						$whereUser = array();
						$whereUser['sector_id'] = array('IN',$arrSector);
						$arrUser = M('User')->where($whereUser)->select();
						$dlsit_user = array();
						$lengthUser = sizeof($arrUser);
						for($j=0;$j<$lengthUser;$j++){
							$dlsit_user[$j] = $arrUser[$j]['user_id'];
						}
						if($dlsit_user){
							$where_mount['b.customer_sales'] = array('IN',$dlsit_user);
						}else{
							$Dlist[$i]['pkid'] = $dlist_pk[$i]['pkid'];
							$Dlist[$i]['a_mount'] = 0;
							$Dlist[$i]['b_mount'] = 0;
							continue;
						}
					}else{
						$Dlist[$i]['pkid'] = $dlist_pk[$i]['pkid'];
						$Dlist[$i]['a_mount'] = 0;
						$Dlist[$i]['b_mount'] = 0;
						continue;
					}
				}
				if($dlist_pk[$i]['b_type'] == 1){
					$where_mount['c.customer_sales'] = $dlist_pk[$i]['b'];
				}else if($dlist_pk[$i]['b_type'] ==2){
					$MSector = M('\Admin\Model\GoalsModel:Sector');
					$arrSector = $MSector->getChild($dlist_pk[$i]['b']);
					if($arrSector){
						$whereUser = array();
						$whereUser['sector_id'] = array('IN',$arrSector);
						$arrUser = M('User')->where($whereUser)->select();
						if($arrUser){
							$dlsit_user = array();
							$lengthUser = sizeof($arrUser);
							for($j=0;$j<$lengthUser;$j++){
								$dlsit_user[$j] = $arrUser[$j]['user_id'];
							}
							$where_mount['c.customer_sales'] = array('IN',$dlsit_user);			
						}else{
							$Dlist[$i]['pkid'] = $dlist_pk[$i]['pkid'];
							$Dlist[$i]['a_mount'] = 0;
							$Dlist[$i]['b_mount'] = 0;
							continue;
						}
					}else{
						$Dlist[$i]['pkid'] = $dlist_pk[$i]['pkid'];
						$Dlist[$i]['a_mount'] = 0;
						$Dlist[$i]['b_mount'] = 0;
						continue;
					}
				}
				$where_mount['a.pkid'] = $dlist_pk[$i]['pkid'];
				$Dlist[$i] = M()->field("a.pkid,a.a_goal,a.b_goal,a.pk_type,a.end_time,sum(b.pay_mount) as a_mount,sum(c.pay_mount) as b_mount")
								->table('sales_pk as a')
								->join('pay_record as b on a.start_time < b.create_time and a.end_time > b.create_time','LEFT')
								->join('pay_record as c on a.start_time < c.create_time and a.end_time > c.create_time','LEFT')
								->where($where_mount)
								->select();
				$Dlist[$i] = $Dlist[$i][0];
				$Dlist[$i]['last_time'] = date('Y-m-d H:i:s');
				$nowtime = date('Y-m-d H:i:s');
				$endtime = $Dlist[$i]['end_time'];
				if($nowtime>=$endtime){
					if($Dlist[$i]['pk_type']==1){
						if($Dlist[$i]['a_mount']>=$Dlist[$i]['b_mount']){
							$Dlist[$i]['pk_res'] = 1;
						}else if($Dlist[$i]['a_mount']<$Dlist[$i]['b_mount']){
							$Dlist[$i]['pk_res'] = 2;
						}else if($Dlist[$i]['a_mount']==$Dlist[$i]['b_mount']){
							$Dlist[$i]['pk_res'] = 3;
						}
					}else if($Dlist[$i]['pk_type']==2){
						if($Dlist[$i]['a_mount']>=$Dlist[$i]['a_goal']){
							if($Dlist[$i]['b_mount']<$Dlist[$i]['b_goal']){
								$Dlist[$i]['pk_res'] = 1;
							}else{
								$Dlist[$i]['pk_res'] = 3;
							}
						}else{
							if($Dlist[$i]['b_mount']<$Dlist[$i]['b_goal']){
								$Dlist[$i]['pk_res'] = 4;
							}else{
								$Dlist[$i]['pk_res'] = 2;
							}
						}
					}
				}else{
					$Dlist[$i]['pk_res'] = 0;
				}
				$dlsit_save[$i] = M('pk','sales_')->save($Dlist[$i]);
			}
			if($dlsit_save){
				$res['rcode'] = 1;
				$res['data'] = $dlsit_save;
			}
		}
		e_json($res);
	}
	/**
	 * 营销目标模块
	 * 更新当前(正在进行的营销目标)续费
	 */
	public function goalMount(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$uinfo =$MTags->index();
		$sector_id = $uinfo['sector_id'];
		$user_id = $uinfo['sector_id'];

		$where_goal = array();
		$where_goal['start_time'] = array('ELT',date('Y-m-d H:i:s'));
		$where_goal['end_time'] = array('EGT',date('Y-m-d H:i:s'));
		$dlist_goal = M('goal','sales_')->where($where_goal)->order('goal_id desc')->select();
		$length_goal = sizeof($dlist_goal);
		if($dlist_goal){
			$Dlist = array();
			$dlsit_save = array();
			for($i=0;$i<$length_goal;$i++){
				$where_mount = array();
				$where_mount['b.audit_res'] = 1;
				$where_mount['a.goal_id'] = $dlist_goal[$i]['goal_id'];
				if($dlist_goal[$i]['target_type'] == 1){
					$where_mount['b.customer_sales'] = $dlist_goal[$i]['target'];
				}else if($dlist_goal[$i]['target_type'] ==2){
					$MSector = M('\Admin\Model\GoalsModel:Sector');
					$arrSector = $MSector->getChild($dlist_goal[$i]['target']);
					$whereUser = array();
					$whereUser['sector_id'] = array('IN',$arrSector);
					$arrUser = M('User')->where($whereUser)->select();
					$dlist_user = array();
					$lengthUser = sizeof($arrUser);
					for($j=0;$j<$lengthUser;$j++){
						$dlist_user[$j] = $arrUser[$j]['user_id'];
					}
					if($dlist_user){
						$where_mount['b.customer_sales'] = array('IN',$dlist_user);
					}
					else{
						$Dlist[$i]['goal_id'] = $dlist_goal[$i]['goal_id'];
						$Dlist[$i]['mount'] = 0;
						continue;
					}
				}
				$Dlistd[$i] = M()->field("sum(b.pay_mount) as achieve,a.goal_id,a.goal")
								->table('sales_goal as a')
								->join('pay_record as b on a.start_time < b.create_time and a.end_time > b.create_time','LEFT')
								->group('a.goal_id')
								->where($where_mount)
								->select();

				$whree[$i]['id'] = $dlist_goal[$i]['goal_id'];
				$Dlist[$i] = $Dlistd[$i][0];
				$Dlist[$i]['last_time'] = date('Y-m-d H:i:s');
				$nowtime = date('Y-m-d H:i:s');
				$endtime = $Dlist[$i]['end_time'];

				if($nowtime>=$endtime){
					if($Dlist[$i]['achieve']>=$Dlist[$i]['goal']){
						$Dlist[$i]['goal_res'] = 1;
					}else{
						$Dlist[$i]['goal_res'] = 2;
					}
				}else{
					$Dlist[$i]['goal_res'] = 0;
				}
				$dlsit_save[$i] = M('goal','sales_')->save($Dlist[$i]);
			}
			if($Dlist){
				$res['rcode'] = 1;
				$res['data'] = $Dlist;
			}
		}
		e_json($res);
	}
		
}